package api;

import java.util.Arrays;
import java.util.Comparator;

public class ArraySortComparator {

	public static void main(String[] args) {
		Hotel[] array = new Hotel[5];
		array[0] = new Hotel(3, "CESAR'S HERITAGE");
		array[1] = new Hotel(5, "CLEOPATRA PALACE");
		array[2] = new Hotel(4, "ANCIENT WONDER");
		array[3] = new Hotel(1, "THE HOUSE");
		array[4] = new Hotel(2, "SWEATODORIUM");
		
		Arrays.sort(array, new HotelComparator(true));
		System.out.println(Arrays.toString(array));
	}
	
	static class HotelComparator implements Comparator<Hotel> {

		boolean isAscending;
		
		public HotelComparator(boolean isAscending) {
			this.isAscending = isAscending;
		}
		
		@Override
		public int compare(Hotel o1, Hotel o2) {
			if (o1.getStars() > o2.getStars()) {
				return (isAscending ? 1 : -1);
			}
			else if (o1.getStars() < o2.getStars()) {
				return (isAscending ? -1 : 1);
			}
			else {
				return 0;
			}
		}

	}

	static class Hotel {

		private int stars;
		private String name;

		public Hotel(int stars, String name) {
			this.stars = stars;
			this.name = name;
		}

		public int getStars() {
			return stars;
		}

		public void setStars(int stars) {
			this.stars = stars;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "Hotel [stars=" + stars + ", name=" + name + "]";
		}

	}
	
}
